package com.bancomer.batch.validators.impl;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.Properties;

import com.bancomer.batch.pojo.FileHelper;
import com.bancomer.batch.pojo.ValidationResult;
import com.bancomer.batch.validators.GeneralValidator;

public class EnableAlertValidator implements GeneralValidator{

	@Override
	public ValidationResult executeValidation(FileHelper fileHelper,
			Object... obj) {
		ValidationResult result = new ValidationResult();
		Properties properties = fileHelper.getProperties();
		FileReader input = null;
		File file = null;
		BufferedReader buffer = null;
		String line = "";
		String disableAlert = (String)properties.getProperty("containsDisableAlert");
		boolean succes = Boolean.valueOf(properties.getProperty(disableAlert));
		try{
			file = new File(fileHelper.getLocalPath()+fileHelper.getName());
			if(file.exists()==true&&file.length()>0){
				input = new FileReader(fileHelper.getLocalPath()+fileHelper.getName());
				buffer = new BufferedReader(input);
				line = buffer.readLine();
				while (line != null)   {
					if(line.contains(disableAlert)){
						result.setMessage("La alerta "+disableAlert.substring(2)+" esta desactivada");
						result.setSucces(succes);
						break;
					}
					line=buffer.readLine();
		    	}
				buffer.close();
			}
		}
		catch(Exception e){
			result.setMessage("Class: EnableAlertValidator - "+e.getMessage());
			result.setSucces(false);
		}
		return result;
	}
 
}
